package com.itheima.pinda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.itheima.pinda.common.CustomIdGenerator;
import com.itheima.pinda.entity.PdTruckDriverLicense;
import com.itheima.pinda.mapper.PdTruckDriverLicenseMapper;
import com.itheima.pinda.service.PdTruckDriverLicenseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PdTruckDriverLicenseServiceImpl implements PdTruckDriverLicenseService {
    @Autowired
    private CustomIdGenerator idGenerator;
    @Autowired
    private PdTruckDriverLicenseMapper truckDriverLicenseMapper;

    @Override
    public PdTruckDriverLicense saveTruckDriverLicense(PdTruckDriverLicense pdTruckDriverLicense) {
        PdTruckDriverLicense driverLicense = truckDriverLicenseMapper.selectOne(new LambdaQueryWrapper<PdTruckDriverLicense>().eq(PdTruckDriverLicense::getUserId, pdTruckDriverLicense.getUserId()));
        if (driverLicense == null) {
            pdTruckDriverLicense.setId(idGenerator.nextId(pdTruckDriverLicense) + "");
        } else {
            pdTruckDriverLicense.setId(driverLicense.getId());
        }
        truckDriverLicenseMapper.insert(pdTruckDriverLicense);
        return pdTruckDriverLicense;
    }

    @Override
    public PdTruckDriverLicense findOne(String userId) {
        LambdaQueryWrapper<PdTruckDriverLicense> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(userId)) {
            lambdaQueryWrapper.eq(PdTruckDriverLicense::getUserId, userId);
        }
        return truckDriverLicenseMapper.selectOne(lambdaQueryWrapper);
    }
}
